﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using eServiceAvailability.NetUtils.Database;
using eServiceAvailability.NetUtils.Database.Model;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.IO;
using System.Web.Configuration;
using System.Globalization;
using log4net;

namespace eServiceAvailability.Availability
{
    /// <summary>
    /// Summary Description for ServiceAvailability
    /// </summary>
    [WebService(Namespace = "http://amdemo.findavailability/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ServiceAvailability : System.Web.Services.WebService
    {
        protected static readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        // return Json -----
        [WebMethod]
        public string FindAvailability(string WorkShopId, string TypeOfService, string Day)
        {
            var json = "";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Hashtable h = new Hashtable();
            try
            {
                List<TimeSlot> list = new List<TimeSlot>();
                CultureInfo provider = CultureInfo.InvariantCulture;


                ErrorObj val = new ErrorObj();
                val.ErrorCode = 0;
                val.Message = "Successful";

                //if (string.IsNullOrEmpty(WorkShopId) || string.IsNullOrEmpty(TypeOfService) || string.IsNullOrEmpty(Day))
                if (string.IsNullOrEmpty(Day))
                {
                    val.ErrorCode = -1;
                    val.Message = "Input Invalid";
                    //json = jss.Serialize(val);
                    //return json;
                }
                else
                {
                    var Settings = WebConfigurationManager.AppSettings;

                    DateTime d = new DateTime();
                    string dateformat = Settings["DateFormat"] == null || Settings["DateFormat"].Trim() == "" ? "yyyy.MM.dd" : Settings["DateFormat"];
                    d = DateTime.ParseExact(Day.Trim(), dateformat, CultureInfo.InvariantCulture);
                    string date = d.ToString("yyyy.MM.dd");

                    list = DBAccess.FindAvailability(TypeOfService, WorkShopId, date);
                }
                h.Add("status", val);
                h.Add("freetime", list);
                json = jss.Serialize(h);
                return json;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message);
                ErrorObj val = new ErrorObj();
                val.ErrorCode = -1;
                val.Message = ex.Message;
                h.Add("status", val);
                h.Add("freetime", "");
                json = jss.Serialize(h);
                return json;


            }
        }

        // return list object-----

        [WebMethod]
        public List<TimeSlot> FindAvailability2(string WorkShopId, string TypeOfService, string Day)
        {
            var json = "";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Hashtable h = new Hashtable();
            try
            {
                List<TimeSlot> list = new List<TimeSlot>();
                CultureInfo provider = CultureInfo.InvariantCulture;


                ErrorObj val = new ErrorObj();
                val.ErrorCode = 0;
                val.Message = "Successful";

                //if (string.IsNullOrEmpty(WorkShopId) || string.IsNullOrEmpty(TypeOfService) || string.IsNullOrEmpty(Day))
                if (string.IsNullOrEmpty(Day))
                {
                    val.ErrorCode = -1;
                    val.Message = "Input Invalid";
                    //json = jss.Serialize(val);
                    //return json;
                }
                else
                {
                    var Settings = WebConfigurationManager.AppSettings;

                    DateTime d = new DateTime();
                    string dateformat = Settings["DateFormat"] == null || Settings["DateFormat"].Trim() == "" ? "yyyy.MM.dd" : Settings["DateFormat"];
                    d = DateTime.ParseExact(Day.Trim(), dateformat, CultureInfo.InvariantCulture);
                    string date = d.ToString("yyyy.MM.dd");

                    list = DBAccess.FindAvailability(TypeOfService, WorkShopId, date);
                }
                h.Add("status", val);
                h.Add("freetime", list);
                
                return list;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message);
                List<TimeSlot> list = new List<TimeSlot>();

                return list;


            }
        }
    }
}
